package com.jhmk.jhecis.controller;

import com.jhmk.jhecis.mapper.InterfaceToolMapper;
import com.jhmk.jhecis.service.impl.InterfaceToolServiceImpl;
import com.jhmk.jhecis.service.common.SqlService;
import com.jhmk.jhecis.utils.JsonUtils;
import com.jhmk.jhecis.utils.ReflectionUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.io.FilenameUtils;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

@RestController
@RequestMapping("test")
@Tag(name="测试Api")
public class TestController {

    @Operation(description = "测试方法")
    @GetMapping("index")
    public String index() {

        return "inidex";
    }
    @Operation(description = "测试hashmap")
    @GetMapping("hashmap")
    public String hashmap() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", "admin");
        map.put("username2", "admin2");
        return JsonUtils.objToStr(map);
    }
    @Operation(description = "接收hashmap2")
    @PostMapping("hashmap2")
    public String hashmap2(@RequestBody HashMap<String, Object> map) {
        for (String key : map.keySet()) {
            System.out.println(key + ":" + map.get(key));
        }
        return "ok";
    }


    @Operation(description = "测试反射")
    @GetMapping("testReflat")
    public String testReflat(){
//        try {
//            Class<?> cs= Class.forName(classname);
//          return cs.getName();
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
        //}
        ApplicationHome home = new ApplicationHome(getClass());//获取jar目录，只有springboot项目能用
        File jarFile = home.getSource();
        String fullName= Paths.get(jarFile.getParentFile().toString(),"imhelper.jar").toAbsolutePath().toString();
        System.out.println(fullName);
        return  ReflectionUtil.Invoke(fullName,"com.jhmk.imhelper.YfcHelper","GetData");

    }

    @Operation(description = "测试属性")
    @GetMapping("reflatFiled")
    public String reflatFiled(){
        try {
            Class<?> cs= Class.forName("com.jhmk.jhecis.domain.DeptDict");
          Field[] Field= cs.getFields();

          for (Field field : Field) {
              field.setAccessible(true);
              field.getType().equals(Integer.class);
          }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }


        return  "ff";
    }

    @Resource
    SqlService sqlService;

    @Resource
    InterfaceToolMapper interfaceToolMapper;

    @Operation(description = "读取外部文集")
    @GetMapping("readExt")
    public String readExt(){
        try {

         return   sqlService.readSqlFromFile("E:\\table.sql");
        } catch (Exception e) {

        }
        return  "ff";
    }

    @Resource
    InterfaceToolServiceImpl interfaceToolService;

    @Operation(description = "执行sql")
    @GetMapping("exesql")
    public List<Map<String, Object>> exesql(){
        return  null;
    }

    @Operation(description = "文件上传")
    @PostMapping("/upload")
    public String handleFileUpload(@RequestParam("file") MultipartFile file) {
        try {
            //ClassUtils.getDefaultClassLoader().getResource("").getPath();//获取jar内部目录
            // 获取文件的原始名称
            String originalFilename = file.getOriginalFilename();
            // 获取文件的后缀名
            String fileExtension = FilenameUtils.getExtension(originalFilename);
            // 生成新的文件名
            String newFilename = UUID.randomUUID().toString() + "." + fileExtension;
            String home = new ApplicationHome(getClass()).getSource().getParent();
            // 创建文件存储路径
            Path uploadPath = Paths.get(home,"uploads" + newFilename);
            System.out.println(uploadPath);
            // 将文件写入到文件系统
            Files.write(uploadPath, file.getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "upload";
    }

}
